using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Win32;

namespace PengYouClient
{
    static public class SettingsManager
    {
        public static string PengYouPath;

        public static string Server_host;
        public static string Server_port;
        public static string Server_user;
        public static string Server_pass;

        public static string Proxy_useproxy;
        public static string Proxy_host;
        public static string Proxy_port;
        public static string Proxy_user;
        public static string Proxy_pass;

        public static string Ressource_basedir;

        public static string language;

        static public bool readXML()
        {
            try
            {
                const string pathKey = "HKEY_CURRENT_USER\\Software\\PengYou";
                PengYouPath = (string)Registry.GetValue(pathKey, "Path", null);
                XmlTextReader reader = new XmlTextReader(PengYouPath + "settings.xml");

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        feelVars(reader);
                    }
                }
                reader.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        static void feelVars(XmlTextReader reader)
        {
            switch (reader.Name)
            {
                case "Server":
                    {
                        reader.MoveToNextAttribute();
                        for (int stop = 0; stop != reader.AttributeCount; stop++, reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                                case "host":
                                    Server_host = reader.Value;
                                    break;
                                case "port":
                                    Server_port = reader.Value;
                                    break;
                                case "user":
                                    Server_user = reader.Value;
                                    break;
                                case "pass":
                                    Server_pass = reader.Value;
                                    break;
                            }
                        }
                        break;
                    }
                case "Ressource":
                    {
                        reader.MoveToNextAttribute();
                        for (int stop = 0; stop != reader.AttributeCount; stop++, reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                                case "basedir":
                                    Ressource_basedir = reader.Value;
                                    break;
                            }
                        }
                        break;
                    }
                case "Proxy":
                    {
                        reader.MoveToNextAttribute();
                        for (int stop = 0; stop != reader.AttributeCount; stop++, reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                                case "useproxy":
                                    Proxy_useproxy = reader.Value;
                                    break;
                                case "host":
                                    Proxy_host = reader.Value;
                                    break;
                                case "port":
                                    Proxy_port = reader.Value;
                                    break;
                                case "user":
                                    Proxy_user = reader.Value;
                                    break;
                                case "pass":
                                    Proxy_pass = reader.Value;
                                    break;
                            }
                        }
                        break;
                    }
                case "Locales":
                    {
                        reader.MoveToNextAttribute();
                        for (int stop = 0; stop != reader.AttributeCount; stop++, reader.MoveToNextAttribute())
                        {
                            switch (reader.Name)
                            {
                                case "language":
                                    language = reader.Value;
                                    break;
                            }
                        }
                        break;
                    }
            }
        }

        static public void UpdateVar(string parentName, string nodeName, string nodeValue)
        {
            XmlDocument doc = new XmlDocument();
            XmlTextReader reader = new XmlTextReader(PengYouPath + "settings.xml");
            doc.Load(reader);
            reader.Close();

            string xmlPath_line = "/PengYou/" + parentName + "/@" + nodeName;
            XmlNode node = doc.SelectSingleNode(xmlPath_line);
            if (node != null)
                node.InnerText = nodeValue;
            else
            {
                XmlNode newNode;
                if ((newNode = doc.SelectSingleNode("//" + parentName)) == null)
                    newNode = doc.CreateElement(parentName);                
                XmlAttributeCollection coll = newNode.Attributes;
                XmlAttribute att = doc.CreateAttribute(nodeName);
                att.Value = nodeValue;
                coll.Append(att);
                doc.DocumentElement.AppendChild(newNode);
            }
            doc.Save(PengYouPath + "settings.xml");       
        }
    }
}
